//
//  CNLoginController.m
//  BeeQuick_19
//
//  Created by 刘家强 on 16/9/24.
//  Copyright © 2016年 CN Team. All rights reserved.
//

#import "CNLoginController.h"
#import <SMS_SDK/SMSSDK.h>

@interface CNLoginController ()
// 手机号
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
// 验证码
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
// 获取短信验证码
@property (weak, nonatomic) IBOutlet UIButton *getPwd;
// 获取语音验证码
@property (weak, nonatomic) IBOutlet UIButton *getVoicePwd;
// 登陆验证
@property (weak, nonatomic) IBOutlet UIButton *logInBut;
// 定时器
@property (nonatomic, strong) NSTimer *timer;
// 记录改变的时间
@property (nonatomic, assign) int  changTime;

@end

@implementation CNLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置定时器的默认时间
    self.changTime = 60;
    
    // 添加点击事件
    [self.getPwd addTarget:self action:@selector(getVerifyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.getVoicePwd addTarget:self action:@selector(getSoundVerifyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.logInBut addTarget:self action:@selector(startVerify:) forControlEvents:UIControlEventTouchUpInside];
    //设置导航栏
    //添加返回按钮
//    self.navigationItem.leftBarButtonItem = [UIBarButtonItem barBuutonItemWithTarget:self action:@selector(backButtonClick) icon:@"v2_goback" highlighticon:@"v2_goback"];
    self.title = @"登录";
    
    [SVProgressHUD setMinimumDismissTimeInterval:1.5];
}

#pragma mark - 按钮点击事件
#pragma mark 获取语音验证
- (void)getSoundVerifyBtnClick:(id)sender
{
    if ([self.phoneTextField.text isEqualToString:@""])
    {
        
        [SVProgressHUD showInfoWithStatus:@"亲,请输入手机号码"];


        return;
    }
    else if (self.phoneTextField.text.length <11)
    {
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码格式不正确"];

        return;
    }
    
    NSLog(@"语音");
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodVoice phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (error)
        {
            NSLog(@"获取验证码失败 error = %@",error);
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];

        }
        else
        {
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];

        }
    }];
    
}

#pragma mark 获取验证码
- (void)getVerifyBtnClick:(id)sender
{
    if ([self.phoneTextField.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入手机号码"];

        return;
    }
    else if (self.phoneTextField.text.length != 11)
    {
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码格式不正确"];

        return;
    }
    
    NSLog(@"短信");
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        if (error)
        {
            NSLog(@"error = %@",error);
            [SVProgressHUD showErrorWithStatus:@"获取验证码失败"];

        }
        else
        {
            [SVProgressHUD showSuccessWithStatus:@"获取验证码成功"];

            //设置按钮的交互事件
            self.getPwd.enabled = NO;
            //设置按钮的标题样式颜色背景
            [self.getPwd setTitle:@"60秒" forState:UIControlStateNormal];
            [self.getPwd setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [self.getPwd setBackgroundColor:[UIColor lightGrayColor]];
            
            //添加动画
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerAnimation) userInfo:nil repeats:YES];
        }
    }];
    
}

#pragma mark 开始验证
- (void)startVerify:(id)sender
{
    NSLog(@"开始验证");
    
    if ([self.phoneTextField.text isEqualToString:@""])
    {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入手机号码"];

        return;
    }
    else if (self.phoneTextField.text.length <11)
    {
        [SVProgressHUD showInfoWithStatus:@"您输入的手机号码格式不正确"];

        return;
    }else if (self.passwordTextField.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"亲,请输入验证码"];

        return;
    }else if (self.passwordTextField.text.length != 4) {
        [SVProgressHUD showInfoWithStatus:@"您输入的验证码格式不正确"];

        return;
    }
    
    [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:self.phoneTextField.text zone:@"86" customIdentifier:nil result:^(NSError *error) {
        
        NSLog(@"%@-%@",self.passwordTextField.text,  self.phoneTextField.text);
        
        if (error)
        {
            NSLog(@"验证失败 error = %@",error);
            [SVProgressHUD showErrorWithStatus:@"验证失败"];

        }
        else
        {
            [SVProgressHUD showSuccessWithStatus:@"验证成功"];
            if (self.loginBlock != nil) {
                self.loginBlock(self.phoneTextField.text);
                // 偏好设置
                [[NSUserDefaults standardUserDefaults] setValue:self.phoneTextField.text forKey:phoneNum];
            }
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
    }];
    
}

#pragma mark - 时间动画的回调
- (void)timerAnimation{
    
    int remainTime = self.changTime -1;
    self.changTime = remainTime;
    
    //倒计时的时间为零时
    if (remainTime == 0.0) {
        //获取验证码的按钮可以点击
        self.getPwd.enabled = YES;
        //重新记录时间
        self.changTime = 60;
        //设置获取验证码按钮的文字,颜色,背景
        [self.getPwd setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.getPwd setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [self.getPwd setBackgroundColor:[UIColor colorWithHex:0xFDC70E]];
        //停止定时器
        [self.timer invalidate];
        return;
    }
    
    [self.getPwd setTitle:[NSString stringWithFormat:@"%d秒",remainTime] forState:UIControlStateNormal];
}

@end
